
const {ccclass, property} = cc._decorator;

import VolumeItem from "../items/VolumeItem";

@ccclass
export default class VolumeTrendComponent extends cc.Component {

    @property(cc.Prefab)
    volumePrefab: cc.Prefab = null;

    @property(cc.Label)
    maxVolume: cc.Label = null;

    @property(cc.Node)
    volumeLayer: cc.Node = null;

    _itemArray: any = [];
    
    clearData () {
        for (let index = 0; index < this._itemArray.length; index++) {
            const item = this._itemArray[index];
            item.node.opacity = 0;
        }
    }

    setData (list, nodeWidth) {
        this.clearData();
        let maxAmount = 0;
        for (let index = 0; index < list.length; index++) {
            const item = list[index];
            if (item.amount > maxAmount) {
                maxAmount = item.amount;
            }
        }
        this.maxVolume.string = Math.ceil(maxAmount / 100000000) + '亿';

        for (let index = 0; index < list.length; index++) {
            const data = list[index];
            let x = nodeWidth * index + nodeWidth;
            let item: VolumeItem = this._itemArray[index];
            if (item == null) {
                let newNode = cc.instantiate(this.volumePrefab);
                this.volumeLayer.addChild(newNode);
                item = newNode.getComponent(VolumeItem);
                this._itemArray[index] = item;
            }
            item.node.opacity = 255;
            if (nodeWidth > 8) {
                item.node.width = nodeWidth - 2;
            } else {
                item.node.width = nodeWidth;
            }
            
            item.node.x = x;
            item.node.y = 0;
            item.setItem(data.change >= 0, data.amount / maxAmount);
        }
    }
}
